/*
 * Copyright 2021-2030 itbox.cn All right reserved. This software is the confidential and proprietary information
 * of itbox.cn ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.cn.
 */
package cn.itbox.example.infrastructure.mysql;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.itbox.uap.db.model.PageResult;
import cn.itbox.example.domain.model.RoleModel;
import cn.itbox.example.domain.repository.RoleRepository;
import cn.itbox.example.infrastructure.mysql.dao.RoleDao;
import cn.itbox.example.infrastructure.mysql.entity.RoleDO;
import cn.itbox.uap.domain.util.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.infrastructure.repository
 * @Description TODO
 * @ClassName RoleRepositoryImpl
 * @Author xingc
 * @Date 2023/11/26 18:53
 * @Version 1.0.0
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Lazy))
public class RoleRepositoryImpl implements RoleRepository {
    private final RoleDao roleDao;

    @Override
    public RoleModel byId(Long id) {
        RoleDO userDO = roleDao.selectById(id);
        return BeanUtil.copyProperties(userDO, RoleModel.class);
    }

    @Override
    public void delete(Long id) {
        roleDao.deleteById(id);
    }

    @Override
    public RoleModel save(RoleModel roleModel) {
        RoleDO roleDO = BeanUtil.copyProperties(roleModel, RoleDO.class);
        if (null == roleDO.getId()) {
            roleDao.insert(roleDO);
            roleModel.setId(roleDO.getId());
        } else {
            roleDao.updateById(roleDO);
        }

        return BeanUtil.copyProperties(roleDO, RoleModel.class);
    }

    @Override
    public List<RoleModel> findAll() {
        List<RoleDO> userDOS = roleDao.selectList(new QueryWrapper<>());
        return userDOS.stream().map(item -> BeanUtil.copyProperties(item, RoleModel.class)).collect(Collectors.toList());
    }

    @Override
    public PageResult<RoleModel> findPage(Long pageIndex, Long pageSize) {
        Page<RoleDO> page = new Page<RoleDO>(pageIndex, pageSize);
        Page<RoleDO> roleDOPage = roleDao.selectPage(page, new QueryWrapper());
        return Converter.mpPage2PageResult(roleDOPage, RoleModel.class);
    }

    @Override
    public PageResult<RoleModel> findPage(String roleName, Long pageIndex, Long pageSize) {
        Page<RoleDO> page = new Page<RoleDO>().setCurrent(pageIndex).setSize(pageSize);
        Page<RoleDO> roleDOPage = roleDao.selectPage(page, new LambdaQueryWrapper<RoleDO>().like(RoleDO::getName, roleName));
        return Converter.mpPage2PageResult(roleDOPage, RoleModel.class);
    }
}
